import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

#年龄分布数据
ages = ['人力部 130', '行政部 226', '财务部 238', '工程部 293', '采购部 326', '销售部 451']
percentages = [12.5, 20.8, 29.2, 37.5, 100, 100]  # 假设新增的两个圆环占比为100%

#自定义颜色
colors = ['#F7C1CF', '#FFD47F', '#7B92C7', '#ADD9EE', '#E47373', '#73E4C7']

#创建图形和坐标轴
fig, ax = plt.subplots(figsize=(10, 10))

#设置环形宽度
ring_width = 0.1

#定义每个圆环的方向
directions = [(10, 90), (-10, 90), (-30, 90), (-50, 90), (-70, 90), (-90, 90)]

#绘制每个年龄段的环形
for i, (age, percent, direction, color) in enumerate(zip(ages[::-1], percentages[::-1], directions[::-1], colors[::-1])):
    #计算内外半径
    inner_radius = (len(ages) - i - 1) * ring_width
    outer_radius = inner_radius + ring_width

    #获取当前圆环的方向
    theta1, theta2 = direction

    #创建环形
    arc = mpatches.Wedge(center=(0, 0), r=outer_radius, width=ring_width,
                         theta1=theta1, theta2=theta2,
                         facecolor=color, edgecolor='black')

    # 添加环形到坐标轴
    ax.add_patch(arc)

    # 添加年龄标签
    age_x = outer_radius * np.cos(np.radians(90))-0.15
    age_y = (outer_radius + inner_radius) / 2
    ax.text(age_x, age_y, age, ha='center', va='center', rotation=0)

#在内部画一个比内圈圆半径小的空白圆
inner_circle_radius = (len(ages) - 1) * ring_width
smaller_inner_circle = mpatches.Circle((0, 0), inner_circle_radius * 0.07,
                                       facecolor='white', edgecolor='none')
ax.add_patch(smaller_inner_circle)

# 设置坐标轴界限，使图形居中
ax.set_xlim(-1 - outer_radius, 1 + outer_radius)
ax.set_ylim(-1 - outer_radius, 1 + outer_radius)

# 设置坐标轴属性
ax.set_aspect('equal')
ax.axis('off')

#plt.savefig('D:/python charm/01/可视化/第二章/图片/跑道图.png')

# 显示图形
plt.show()